// pages/today/today.js
const time = require('../../utils/time.js')
const app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    region: ['不限','不限','不限'],
    time: '',
    timeData: {},
    status: '',
    interval: '',
    date: null,
    page_token: 0,
    myl: false,
    customItem: '不限'
  },
  navTo: function (e) { //跳转
    var url = e.currentTarget.dataset.url;
    var status = e.currentTarget.dataset.status;
    if (status == '优选') {
      status = 0
    } else {
      status = 1
    }
    wx.navigateTo({
      url: url + '?status=' + status
    })
  },
  onChange(e) {
    this.setData({
      timeData: e.detail,
    });
  },
  bindDateChange(e) {
    this.setData({
      date: e.detail.value,
    })
    Object.assign(this.data,{
      page_token: 0
    })
    let data = {
      page_size: 10,
      page_token: 0
    }
    this.api(data)
  },
  bindRegionChange: function (e) {
    this.setData({
      region: e.detail.value,
    })
    Object.assign(this.data,{
      page_token: 0
    })
    let data = {
      page_size: 10,
      page_token: 0
    }
    this.api(data)
  },
  
  api(data) {
    let that = this;
    let status = that.data.status;
    let listTypes = ['', 'deal_today', 'added_today', 'failed_today', 'no_participant']
    var listType = listTypes[2]
    if (that.data.date) data.date = that.data.date
    console.log(that.data.region)
    if (that.data.region) {
      if (that.data.region[0] != '不限') data.province = that.data.region[0]
      if (that.data.region[1] != '不限') data.city = that.data.region[1]
      if (that.data.region[2] != '不限') data.district = that.data.region[2]
    }
    data.statusArr = '预告中,进行中'
    app.ports.today({
      data,
      listType
    }).then((res) => {
      var res = res.data;
      if (data.page_token >= 1) {
        res.results.forEach(item => {
          item.url = "../details/details?status=0&&id=" + item.id
          switch (item.stage) {
            case '变卖':
              item.stage = '秒杀'
              break;
            case '一拍':
              item.stage = '一轮'
              break;
            case '二拍':
              item.stage = '二轮'
              break;
          }
          item.tags = [{
            name: item.builtUpAreaSquareCentimeter + '㎡'
          }, {
            name: item.discountRate + '折'
          }, {
            name: item.stage
          }]
          item.marketPriceYuan = Math.floor((item.marketPriceYuan / 10000));
          item.estimatePriceYuan = Math.floor((item.estimatePriceYuan / 10000));
          item.priceYuan = Math.floor((item.priceYuan / 10000));
          item.dealPrice = Math.floor((item.dealPrice / 10000));
          item.startTime = app.timeFormat(item.startTime)
          item.endTime = app.timeFormat(item.endTime)
          that.data.list.results.push(item);
        });
        res.results = that.data.list.results
      } else {
        res.results.forEach(item => {
          switch (item.stage) {
            case '变卖':
              item.stage = '秒杀'
              break;
            case '一拍':
              item.stage = '一轮'
              break;
            case '二拍':
              item.stage = '二轮'
              break;
          }
          item.url = "../details/details?status=0&&id=" + item.id
          item.tags = [{
            name: item.builtUpAreaSquareCentimeter + '㎡'
          }, {
            name: item.discountRate + '折'
          }, {
            name: item.stage
          }]
          item.startTime = app.timeFormat(item.startTime)
          item.endTime = app.timeFormat(item.endTime)
          item.marketPriceYuan = Math.floor((item.marketPriceYuan / 10000));
          item.dealPrice = Math.floor((item.dealPrice / 10000));
          item.priceYuan = Math.floor((item.priceYuan / 10000));
          item.estimatePriceYuan = Math.floor((item.estimatePriceYuan / 10000));
        })
      }
      // if (res.totalThatMonth <= 9) res.totalThatMonth = '0' + res.totalThatMonth
      // if (res.total <= 9) res.total = '0' + res.total
      if (res.province) that.data.region = [res.province, res.city, res.district]
      that.setData({
        list: res,
        region: that.data.region
      })
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var status = Number(options.status)
    that.data.region[0]=options.province
    that.data.region[1]=options.city
    that.data.region[2]=options.district

    // var titles = ['', '今日成交', '今日新增', '今日结束', '无人报名']
    // var types = ['', '成交', '新增', '结束', '无人报名']
    var titles = ['在线房源', '今日成交', '今日新增', '今日结束', '今日预约']
    var types = ['在线房源', '成交', '新增', '结束', '预约']
    wx.setNavigationBarTitle({
      title: titles[status]
    })
    that.setData({
      date: time.timeHandle(null, 0),
      status: status,
      types: types,
      region: that.data.region
    })
    let data = {
      page_size: 10,
      page_token: 0,
      date: time.timeHandle(null, 0)
    }
    wx.getLocation({
      type: 'gcj02',
      success(res) {
        data.latitude = res.latitude
        data.longitude = res.longitude
        that.api(data)
      },
      fail(res) {
        that.api(data)
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {


  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    clearInterval(this.data.interval)
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    clearInterval(this.data.interval)
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() { //上拉加载更多
    var that = this;
    //检查是否还有数据可以加载
    var total_size = that.data.list.total;
    var size = that.data.list.results.length;
    var page_token = that.data.page_token += 1
    that.data.page_token = page_token;
    //如果还有，则继续加载
    if (total_size > size) {
      var data = {
        page_size: 10,
        page_token: page_token
      }
      that.api(data)
      //如果没有了，则停止加载，显示没有更多内容了
    } else {
      that.setData({
        myl: true,
      })
    }
  },

  
})